/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.indicator;

import com.bumptech.glide.Glide;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * BannerBgView
 *
 * @since 2021-07-29
 */
public class BannerBgView extends RevealLayout implements Component.DrawTask {
    private static final int NUM_560 = 560;
    private Image imageView;
    private Path mClipPath;
    private PixelMapHolder pixelMapHolder;
    private DisplayAttributes attributes;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public BannerBgView(Context context) {
        super(context);
        addImageView(context);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 属性
     */
    public BannerBgView(Context context, AttrSet attrs) {
        super(context, attrs);
        addImageView(context);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 属性
     * @param styleName 样式名
     */
    public BannerBgView(Context context, AttrSet attrs, String styleName) {
        super(context, attrs, styleName);
        addImageView(context);
    }

    /**
     * 添加图片
     *
     * @param context 上下文
     */
    public void addImageView(Context context) {
        addDrawTask(this::onDraw);
        attributes = DisplayManager.getInstance().getDefaultDisplay(getContext()).get().getRealAttributes();
        mClipPath = new Path();
        imageView = new Image(context);
        imageView.setHeight(NUM_560);
        imageView.setWidth(attributes.width);
        imageView.setScaleMode(Image.ScaleMode.CLIP_CENTER);
        this.addComponent(imageView);
    }

    /**
     * 获取图片
     *
     * @return Image
     */
    public Image getImageView() {
        return imageView;
    }

    /**
     * 修改图片
     *
     * @param ob
     * @return Image
     */
    public Image setImageView(Object ob) {
        Glide.with(mContext).load(ob).centerCrop().into(imageView);
        return imageView;
    }

    /**
     * 重置背景图片
     *
     * @param pixelMapHolder1 PixelMapHolder
     */
    public void setImageHolder(PixelMapHolder pixelMapHolder1) {
        if (imageView.getPixelMapHolder() != null) {
            imageView.setPixelMapHolder(null);
        }
        if (this.pixelMapHolder == null) {
            this.pixelMapHolder = pixelMapHolder1;
        }
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        mClipPath.reset();
        mClipPath.addCircle(getClipCenterX(), getClipCenterY(), getClipRadius(), Path.Direction.CLOCK_WISE);
        canvas.save();
        canvas.clipPath(mClipPath, Canvas.ClipOp.INTERSECT);
        if (pixelMapHolder != null) {
            canvas.drawPixelMapHolder(pixelMapHolder, 0, 0, new Paint());
        }
        canvas.restore();
    }
}
